package com.baidu.config;

import com.baidu.controller.BaiDuController;
import com.baidu.controller.IndexController;
import com.baidu.controller.UserController;
import com.baidu.interceptor.LoginInterceptor;
import com.baidu.interceptor.MainInterceptor;
import com.baidu.util.PluginSet;
import com.baidu.util.TestUtil;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class MainConfig extends JFinalConfig {
	
	/**
	 * 加载配置文件
	 */
	public static final Prop custom_prop = PropKit.use("custom.properties");//用户配置
	
	public static final Prop jdbc_prop = PropKit.use("jdbc.properties");//jdbc配置文件
	
	public static final Boolean DEBUG = custom_prop.getBoolean("debug");
	
	public static QuartzManager jobManager;//任务管理

	public static DruidPlugin druidPlugin;//数据源插件
	
	public static ActiveRecordPlugin arp;
	
	@Override
	public void configConstant(Constants me) {
		/*loadPropertyFile("jdbc.properties");*/
		if(DEBUG==true){
			me.setDevMode(true);
		}
		me.setEncoding("utf-8");
		me.setViewType(ViewType.FREE_MARKER);
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class, "/html");	// 第三个参数为该Controller的视图存放路径
		me.add("/user", UserController.class,"/html/user");
		me.add("/baidu", BaiDuController.class,"/html/baidu");
	}

	@Override
	public void configHandler(Handlers me) {
//		me.add(new HttpUrlHandler());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor()); //解决freemarker取不到session问题，还要在handler中设置一下。
		me.add(new MainInterceptor());
		me.add(new LoginInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		/*//加载配置数据源文件
		DruidPlugin druidPlugin = new DruidPlugin(jdbc_prop.get("jdbcUrl"), jdbc_prop.get("username"), jdbc_prop.get("password"));
		me.add(druidPlugin);
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(druidPlugin,SimpleNameStyles.LOWER_UNDERLINE);
		atbp.autoScan(false);
		if(DEBUG==true){
			atbp.setShowSql(true);
		}
		me.add(atbp);*/
		//配置缓存
		me.add(new EhCachePlugin());
		//配置定时任务
	}
	

	@Override
	public void afterJFinalStart() {
		TestUtil.systemOutPrintln("Jfinal Start");
		PluginSet.dataSourceSet();
		PluginSet.quartzSet();
	}
}

















